<?php

class AuthACLAction extends Action
{


	
    /**
     * AuthACLAction::index()
     * 
     * @return
     */
    public function index()
    {
		//$this->assign('baseUrl', "http://www.taogezi.org");
		$this->assign('baseUrl', "http://localhost/tgz");		
	    $this->display('AuthACL:index');
    }

    /**
     * AuthACLAction::auth()
     * 
     * @return
     */
    public function auth()
    {
		$this->assign('baseUrl', "http://localhost/tgz");		
	    $this->display('AuthACL:index');
    }
/**
 * AuthACLAction::insert()
 * 
 * @return
 */
public function insert()
    {
        header('Content-Type:text/html; charset=utf-8'); //防止出现乱码
        if (empty($_POST['user_id']))
        {
            $this->error('帐号错误！');
        } elseif (empty($_POST['password']))
        {
            $this->error('密码必须！');
        } elseif (empty($_POST['verify']))
        {
            $this->error('验证码必须！');
        } elseif ($_SESSION['verify'] != md5($_POST['verify']))
        {
            $this->error('验证码错误！');
        }

        $User = M('User');
        {
            $uid = $User->where('login_id = ' . "'" . $_POST['user_id'] . "'")->find();
            if ($uid)
            {
                $this->error('注册的id已经存在');
            }
            
            $uid1 = $User->where('email = ' . "'" . $_POST['email'] . "'")->find();
            if ($uid1)
            {
                $this->error('注册的email已被占用');
            }            
        }

		$User = M("User"); 		
		$data['login_id'] = $_POST['user_id'];
		$data['password'] = $_POST['password'];
		$data['email'] = $_POST['email'];
        $data['invite_by_id'] = $_POST['invite_by_id'];	
        $data['regtime'] = date("Y-m-d H:i:s");
		$topicid = $User->add($data);

        if ($topicid)
        {
            $role = $User->where('id = ' . $topicid)->find();
            Session::set('UserInfo', $role);

            $url = __APP__.'/Index/panel';
            header("Location: " . $url);
        } else
            $this->error("<script>alert('数据库添加失败');history.back();</script>");
    }
	


    // 登录检测
    /**
     * AuthACLAction::checkLogin()
     * 
     * @return
     */
    public function checkLogin()
    {   
		$this->assign('jumpUrl', "__APP__/AuthACL/login");	
        if (empty($_POST['user_id']))
        {
            $this->error('帐号为空错误！');
        } elseif (empty($_POST['password']))
        {
            $this->error('密码必须！');
        } elseif (empty($_POST['verify']))
        {
            $this->error('验证码必须！');
        } elseif ($_SESSION['verify'] != md5($_POST['verify']))
        {
            $this->error('验证码错误！');
        }
        //检查登陆id和捆绑的密码对应记录是否存在
        $userid = $_POST['user_id'];
        $password = $_POST['password'];
        $User = D('User');
        $role = $User->where('login_id =' . '"' . $userid . '"' . ' and password =' . '"' .
            $password . '"')->find();
        $ncd =     getcwd();        

        if($role)
        {
        $User_info = D('User_info');
        $role1 = $User_info->where('id_user =' . '"' . $role['id'] . '"' )->find();

		$Team = D('Team');
        $teamRole = $Team->where('id_gezi_team =' . '"' . $role['team_id'] . '"' )->find();

		$teamId__ = (isset($role['team_id']))?$role['team_id']:false;//获得本人的团队编号	
			
		$secL=$role['secLevel'];	
		setcookie('secLevel__', $secL, time() + 3600 * 24 * 7, '/');		
		setcookie('teamId__', $teamId__, time() + 3600 * 24 * 7, '/');
        setcookie('id_user__', $role['id'], time() + 3600 * 24 * 7, '/');
        setcookie('person_score__', $role1['person_score'], time() + 3600 * 24 * 7, '/');
        setcookie('person_capcity__', $role1['person_capcity'], time() + 3600 * 24 * 7, '/');	
        setcookie('person_now_capcity__', $role1['person_now_capcity'], time() + 3600 * 24 * 7, '/');
        
        Session::set('UserInfo', $role); //user表                
		Session::set('UserInfo1', $role1);//user_info表
		Session::set('UserInfo2', $teamRole); //团队表
        //dump($_SESSION['UserInfo2']);		
            if ($_POST['checked'] == 'true')
            {
                setcookie('keepLogin', 'true', time() + 3600 * 24 * 7, '/');
                setcookie('password', $_POST['password'], time() + 3600 * 24 * 7, '/');
                setcookie('user_id', $_POST['user_id'], time() + 3600 * 24 * 7, '/');
                setcookie('id_user__',$role['id'], time() + 3600 * 24 * 7, '/');
                
            }
			
            $url = __APP__."/Index/panel";
            header("Location: $url");
        } else
        {
            $this->error('用户不存在或密码错误，登陆出错~');
        }

    }

    /**
     * AuthACLAction::ajaxLogin()
     * 
     * @return
     */
    public function ajaxLogin()
    {
        $this->display('Public:alogin');
    }

    /**
     * AuthACLAction::_ajaxLogin()
     * 
     * @return
     */
    public function _ajaxLogin()
    {

        $user_id = $_GET['userid'];
        $password = $_GET['password'];
		
        $User = D('User');
        $role = $User->where("login_id = '$user_id' and password ='$password'")->find();
        $User_info = D('User_info');
        $role1 = $User_info->where('id_user =' . '"' . $role['id'] . '"' )->find();
        
		$Team = D('Team');
        $teamRole = $Team->where('id_gezi_team =' . '"' . $role['team_id'] . '"' )->find();

		$teamId__ = (isset($role['team_id']))?$role['team_id']:false;//获得本人的团队编号	      
        if ($role)
        {
                Session::set('UserInfo', $role);
                Session::set('UserInfo1', $role1);                
                Session::set('UserInfo2', $teamRole); //团队表
	            echo ajaxrt('ok','自动登陆ok',$role);
        } else
        {
			echo ajaxrt('error','自动登陆出错'.$user_id,$role);
        }

    }

    /**
     * AuthACLAction::regUser()
     * 
     * @return
     */
    public function reg()
    {
        $invite_by_id = isset($_GET['id'])?$_GET['id']:0;
        $md5name = isset($_GET['code'])?$_GET['code']:0;
        if($invite_by_id!=0)
        {
            $role = getUser($invite_by_id);
            $md5role = md5($role['name']);
            if($md5role==$md5name)
            {
            $this->assign('invite_by_id',$invite_by_id);    
            $this->display('reg');        
            }
            else
            $this->error('邀请Url不正确');            
        }
        else
        {
        $this->assign('invite_by_id',0);
        $this->display('reg');        
        } 

    }

    //退出登录
    /**
     * AuthACLAction::logout()
     * 
     * @return
     */
    public function logout()
    {

        Session::clear();
        setcookie("keepLogin", 'false', time() - 3600,'/');
        setcookie("user_id", '', time() - 3600,'/');        
        setcookie("password", "", time() - 3600,'/');
     	setcookie('__IP',"",  time() - 3600,'/');     
	    setcookie('__NOW_APP',"",time() - 3600,'/');
		setcookie('teamId__',"",time() - 3600,'/');
        setcookie('id_user__',"",time() - 3600,'/');
        setcookie('person_capcity__',"",time() - 3600,'/');
        setcookie('person_score__',"",time() - 3600,'/');
        setcookie('secLevel__',"",time() - 3600,'/');
	
        
        		  		
        $url = __APP__ . '/AuthACL/login';
        header("Location: " . $url);
    }
		
//卖家认证

/**
 * AuthACLAction::saleAuth()
 * 
 * @return
 */
public function saleAuth()
    {
        checkLoginSession();//未登录的不能进行卖家验证
		$this->assign('jumpUrl', "__APP__/Index/panel?p=team");			
		//获得用户更多数据，update user表，insert user_info表，ok生成完整的session
		$User = M("User"); 	
		$User_info = M("User_info");
			
		$data['shop_url'] = $_POST['shop_url'];
		$data['category'] = $_POST['category'];
		$data['wwqq'] = $_POST['wwqq'];
		$data['privateqq'] = $_POST['privateqq'];
		$data['city'] = $_POST['city'];
		$data['zip_code'] = $_POST['zip_code'];
		$data['name'] = $_POST['name'];
		$data['mobile'] = $_POST['mobile'];
		$data['sendAdd'] = $_POST['sendAdd'];
		$data['email'] = $_POST['email'];
		$data['jointime'] = date("Y-m-d");
        $data['add_loc'] = $_POST['add_loc'];				
		$topicid =$User->where('id='.$_SESSION['UserInfo']['id'])->save($data);

        if ($topicid)
        {
				$data1['pricePerCust'] = $_POST['pricePerCust'];
				$data1['tenDayNum'] = $_POST['tenDayNum'];
				$data1['monNum'] = $_POST['monNum'];
				$data1['monFee'] = $_POST['monFee'];
				$data1['halfYearNum'] = $_POST['halfYearNum'];				
				$data1['dayZTC'] = $_POST['dayZTC'];
				$data1['id_user'] = $_SESSION['UserInfo']['id'];				
				$topicid1 = $User_info->add($data1);
		
				if ($topicid1)
				{
					setcookie('secLevel__', 0, time() + 3600 * 24 * 7, '/');
					setSecLevel(0);//设置状态为等候卖家认证通过
					$msgkind='SalerApply';
					addMsg($msgs,$from_id_user,$target,$msgkind,$method,$at1,$at2,$at3,$at4,$at5);//添加一条消息记录
					
					$this->success("验证信息已经提交！");
				} else
					$this->error("<script>alert('数据库添加失败');history.back();</script>");	
        } else
            $this->error("<script>alert('数据库添加失败');history.back();</script>");
	
    }
	
	
	/**
	 * AuthACLAction::passSaleAuth()
	 * 
	 * @return
	 */
	protected  function passSaleAuth()//客服联系卖家，人工验证后手工通过卖家认证，执行这个函数
	{
		$User_info = M("User_info");
		$data['accountKind'] = "Saler";//通过认证				
		$topicid =$User_info->where('id='.$_POST['to_auth_id'])->save($data);				
	}
	
		/**
		 * AuthACLAction::setTmember()
		 * 
		 * @return
		 */
		protected  function setTmember()//客服联系卖家，人工验证后手工通过卖家认证，执行这个函数
	{
		$User_info = M("User_info");
		$data['accountKind'] = "Saler";//通过认证				
		$topicid =$User_info->where('id='.$_POST['to_auth_id'])->save($data);				
	}
	/**
	 * AuthACLAction::teamCApply()
	 * 
	 * @return
	 */
	public function teamCApply(){
	 $this->display('teamCApply');
	}	
	
	/**
	 * AuthACLAction::getTeamList()
	 * 
	 * @return
	 */
	public function getTeamList(){
    $id_user = $_SESSION['UserInfo']['zip_code'];
	//$_SESSION['UserInfo']['id'];
	dump($_SESSION['UserInfo']);
	//dump(getZipTeam());
	}
}
?>